package com.itheima.controller;

import com.itheima.advice.OneAnnotation;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryPaeam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

//@Scope("prototype")
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    public ClazzController() {
        System.out.println("ClazzController constructor..................................");
    }

    @GetMapping
    public Result page(ClazzQueryPaeam clazzQueryPaeam ) {
        PageResult pageResult = clazzService.page(clazzQueryPaeam);
        return Result.success(pageResult);
    }

    @OneAnnotation
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        clazzService.deleteById(id);
        return Result.success();
    }

    @OneAnnotation
    @PostMapping
    public Result add(@RequestBody Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzService.save(clazz);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result seleteId(@PathVariable Integer id) {
        Clazz clazz = clazzService.seleteId(id);
        return Result.success(clazz);
    }

    @OneAnnotation
    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzService.updateById(clazz);
        return Result.success();
    }

    @GetMapping("/list")
    public Result findAll() {
        List<Clazz> clazzList = clazzService.list();
        return Result.success(clazzList);
    }
}
